package gogogo.liwushuo.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.hdodenhof.circleimageview.CircleImageView;
import gogogo.liwushuo.R;
import gogogo.liwushuo.adapter.RecyclerAuthorAdapter;
import gogogo.liwushuo.bean.AuthorBean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.listener.OnItemClickListener;
import gogogo.liwushuo.util.OKHttpUtils;

public class HomeAuthorctivity extends AppCompatActivity {

    @InjectView(R.id.author_icon)
    CircleImageView authorIcon;
    @InjectView(R.id.author_name)
    TextView authorName;
    @InjectView(R.id.author_bcg)
    RelativeLayout authorBcg;
    @InjectView(R.id.toolbar_author)
    Toolbar toolbarAuthor;
    @InjectView(R.id.recyclerview)
    RecyclerView recyclerview;
    @InjectView(R.id.cospslayout)
    CollapsingToolbarLayout cospslayout;
    @InjectView(R.id.back)
    ImageView back;
    private int authorId;
    private OKHttpUtils okHttpUtils;
    private Activity mContext;
    private List<AuthorBean.DataBean.PostsBean> posts;
    private RecyclerAuthorAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homr_authorctivity);
        ButterKnife.inject(this);

        initData();
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new RecyclerAuthorAdapter(mContext, posts);
        recyclerview.setAdapter(adapter);
        getJsonData();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getJsonData() {

        okHttpUtils.getAsyncMainData(Resource.getAuthorIntentUrl(authorId), new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                final AuthorBean authorBean = new Gson().fromJson(result, AuthorBean.class);
                authorName.setText(authorBean.getData().getNickname());
                toolbarAuthor.setTitle("");
                Picasso.with(mContext)
                        .load(authorBean.getData().getAvatar_url())
                        .into(authorIcon);
                posts = authorBean.getData().getPosts();
                adapter.setList(posts);
//                Drawable.createFromStream()
                adapter.setOnAuthorItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClickListener(int position) {
                        AuthorBean.DataBean.PostsBean postsBean = posts.get(position);
                        int id = postsBean.getId();
                        Intent intent = new Intent(mContext,HomeImageActivity.class);
                        intent.putExtra("id",id);
                        startActivity(intent);
                    }
                });
            }
        });
    }

    private void initData() {
        mContext = this;
        Intent intent = getIntent();
        if (intent != null) {
            authorId = intent.getIntExtra("id", 0);
        }
        okHttpUtils = OKHttpUtils.newInstance(this);
        cospslayout.setExpandedTitleColor(0);
        cospslayout.setCollapsedTitleTextColor(Color.WHITE);
    }

    //将图片模糊化做背景图片的处理，技术原因，未能实现。
//    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
//    public Bitmap blurBitmap(Bitmap bitmap) {
//
//        //Let's create an empty bitmap with the same size of the bitmap we want to blur
//        Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
//
//        //Instantiate a new Renderscript
//        RenderScript rs = RenderScript.create(mContext);
//
//        //Create an Intrinsic Blur Script using the Renderscript
//        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
//
//        //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
//        Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
//        Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
//
//        //Set the radius of the blur
//        blurScript.setRadius(25.f);
//
//        //Perform the Renderscript
//        blurScript.setInput(allIn);
//        blurScript.forEach(allOut);
//
//        //Copy the final bitmap created by the out Allocation to the outBitmap
//        allOut.copyTo(outBitmap);
//
//        //recycle the original bitmap
//        bitmap.recycle();
//
//        //After finishing everything, we destroy the Renderscript.
//        rs.destroy();
//
//        return outBitmap;
//
//
//    }
}


